// JavaScript Document
$(document).ready(
	function(){
		$('.admins_items_title').click(
			function(){
				$('.admins_items_title').next().slideUp('fast');
				$(this).next().slideToggle('fast');
			}							   
		);
		$('.admins_items_menu a').click(
			function(){
				$('.admins_right').append("<div id='loadingDiv' style='position:absolute;top:5px;left:760px;'><img src='html/images/loading.gif' /></div>");
				$("#admins_frame").css("height","500px");
				setTimeout(function(){$('#loadingDiv').remove()},3000);
			}										  
		);
		$('#admins_frame').load(
			function(){
				$('#loadingDiv').remove();
				commonEffection.resetFrame('admins_frame',false);
			}
		);		
		commonEffection.resetFrame('admins_frame',false);
	}				  
);
var commonEffection={
	setLoading:function(){
		var c = $(window.parent.document.body);
		c.find('.admins_right').append("<div id='loadingDiv' style='position:absolute;top:5px;left:760px;'><img src='html/images/loading.gif' /></div>");
		setTimeout(function(){$('#loadingDiv').remove()},10000);
	},
	confirmBack:function(){
		if(confirm("确定放弃本页面返回前一页?")){
			commonEffection.setFrameHeight(500);
			history.back(-1);
		}
	},
	changePackSell:function(mode,thisInput){
		if(mode=='1'){
			if(!$(thisInput).next().next().is("span")){
				$(thisInput).next().after('<span>&nbsp;( 每包产品 <input type="text" style="width:50px" value="1" name="counts_per_pack" /> 个 ) </span>');
				packCountCheck(new validateProduct());
			}
		}else if(mode=='0'){
			$(thisInput).parent().find('span').remove('span');
		}
	},
	addPrice:function(thisInput){
		$(thisInput).parent().parent().prev().after('<tr><td class="le" style="text-align:center">&nbsp;</td><td class="le" style="text-align:center"><input type="text" name="min_counts[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="max_counts[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="supply_price[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="net_price[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="delivery_days[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="button" onclick="commonEffection.removeThisTr(this)" value="删除" /></td></tr>');
		commonEffection.changeFrameHeight(50);
		priceCheck(new validateProduct());
	},
	addPattern:function(thisInput){
		$(thisInput).parent().parent().prev().after('<tr><td class="le" style="text-align:center"><input type="text" name="pattern_name[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="min_counts[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="max_counts[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="supply_price[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="net_price[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="delivery_days[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="button" onclick="commonEffection.removeThisTr(this)" value="删除" /></td></tr>');
		commonEffection.changeFrameHeight(50);
		priceCheck(new validateProduct());
	},
	removeThisTr:function(thisInput){
		$(thisInput).parent().parent().remove();
		commonEffection.changeFrameHeight(-50);
	},
	removeThisTrAjax:function(thisInput){
		if(!confirm("确定要删除吗?")){
			return false;	
		}
		parent.showMsg('<img src="html/images/loading.gif" />');
		var priceid=$(thisInput).parent().parent().find('input[name="priceid[]"]').val();
		$.post(ROOT_PATH+"/admins"+CHOSING_DE+"product"+CHOSING_DE+"delpriceajax",{priceid:priceid},function(data) {
			if($('input[name="min_counts[]"]').size()==1){
				thisInput.onclick=function(){};
				$(thisInput).parent().parent().find('input[type!="button"]').val('');
				$(thisInput).parent().parent().find('input[name="priceid[]"]').remove();
			}else{
				$(thisInput).parent().parent().remove();
				commonEffection.changeFrameHeight(-50);					
			}
			if(data=="success"){
				parent.shutMsg();
			}else{
				parent.alterMsg('删除失败!');
			}
		});	
	},
	InserImg:function(url){
		var oEditor = FCKeditorAPI.GetInstance('FCKeditor1');
		oEditor.InsertHtml("<img src='"+url+"'>");
	},
	changeFrameHeight:function(offset){
		var height=$(parent.document.getElementById("admins_frame")).css("height");
		$(parent.document.getElementById("admins_frame")).css("height",parseInt(height)+offset+'px');	
	},
	setFrameHeight:function(height){
		$(parent.document.getElementById("admins_frame")).css("height",height+'px');
	},
	resetFrame:function(id,isFrame){
		if(isFrame){
			var bobo=parent.document.getElementById(id);
		}else{
			var bobo=document.getElementById(id);
		}
		if (document.getElementById){
		  if (bobo && !window.opera){
			  if (bobo.contentDocument && bobo.contentDocument.body.offsetHeight){
				bobo.height = bobo.contentDocument.body.offsetHeight;
				$(bobo).css('height',bobo.contentDocument.body.offsetHeight);
			  }
			  else if(bobo.Document && bobo.Document.body.scrollHeight){
				bobo.height = bobo.Document.body.scrollHeight;
				$(bobo).css('height',bobo.Document.body.scrollHeight);
			  }			 
		  }
		}
	}
}
/*
var fenleiEffection={
	showOrHide:function(thisA,flid){
		var smallChildDiv=$(thisA).parent().parent().children('.smallChildDiv');
		if(smallChildDiv.size()==0){
			this.boldOrNormal(thisA,flid);
		}else{
			smallChildDiv.slideToggle('fast');
		}
	},
	boldOrNormal:function(thisA,flid){
		$(thisA).click(
			function(){
				$('#productLeft a').css('fontWeight','normal');
				//$('#productLeft a').css('fontSize','13px');
				$(this).css('fontWeight','bold');
				//$(this).css('fontSize','15px');
				fenleiEffection.showItems(thisA,flid);
			}
		);			
	},
	showItems:function(thisA,flid){
		$('#productRight').html($(thisA).html());
		productEffection.showAll(1,flid);
	},
	getChildFenlei:function(thisSelect){
		$('#flid').val(0);
		$(thisSelect).after('<img id="flloading" style="margin-left:5px" src="html/images/admins/fl_loading.gif" />');
		var pid=$(thisSelect).val();
		if(pid!=0){
			$.post(ROOT_PATH+"/adminb"+CHOSING_DE+"demand"+CHOSING_DE+"getfenleiajax",{pid:pid},function(data) {
					$('#flloading').remove();
					eval("var flArray="+data);
					if(flArray.length!=0){
						var selectString=' <select onchange="fenleiEffection.getChildFenlei(this);"><option value="0">请产品选择分类</option>';
						for(var i=0;i<flArray.length;i++){
							selectString+="<option value="+flArray[i].flid+">"+flArray[i].flname+"</option>";
						}
						selectString+='</select> ';
						fenleiEffection.removeSelect(thisSelect);
						$(thisSelect).after(selectString);
					}else{
						fenleiEffection.removeSelect(thisSelect);
						$('#flid').val(pid);
					}
			});	
		}else{
			$('#flloading').remove();
			fenleiEffection.removeSelect(thisSelect);
		}
	},
	removeSelect:function(thisSelect){
		if($(thisSelect).next().is('select')){
			var nextSelect=$(thisSelect).next();
			var removeSelect=new Array();
			removeSelect.push(nextSelect);
			while(nextSelect.next().is('select')){
				nextSelect=nextSelect.next();
				removeSelect.push(nextSelect);
			}
			for(var i in removeSelect){
				removeSelect[i].remove();
			}
		}			
	}
}*/
var fenleiEffection={
	type:'product',
	optionString:'SELECT',
	showOrHide:function(thisA,flid){
		var smallChildDiv=$(thisA).parent().parent().children('.smallChildDiv');
		if(smallChildDiv.size()==0){			
			this.boldOrNormal(thisA,flid);			
			commonEffection.resetFrame('admins_frame',true);			
		}else{
			smallChildDiv.slideToggle('fast',function(){commonEffection.resetFrame('admins_frame',true);});
		}
	},
	boldOrNormal:function(thisA,flid){				
		$(this).css('fontWeight','bold');
		fenleiEffection.showItems(thisA,flid);
	},
	showItems:function(thisA,flid){
		$('#productRight').html($(thisA).html());		
		productEffection.showAll(1,flid);
	},
	getChildFenlei:function(thisSelect){
		$('#flid').val(0);
		$(thisSelect).after('<img id="flloading" style="margin-left:5px" src="html/images/admins/fl_loading.gif" />');
		var pid=$(thisSelect).val();
		if(pid!=0){
			$.post(ROOT_PATH+"/adminb"+CHOSING_DE+fenleiEffection.type+CHOSING_DE+"getfenleiajax",{pid:pid},function(data) {
					$('#flloading').remove();
					eval("var flArray="+data);
					if(flArray.length!=0){
						var selectString=' <select onchange="fenleiEffection.getChildFenlei(this);"><option value="0">'+fenleiEffection.optionString+'</option>';
						for(var i=0;i<flArray.length;i++){
							selectString+="<option value="+flArray[i].flid+">"+flArray[i].en_flname+"</option>";
						}
						selectString+='</select> ';
						fenleiEffection.removeSelect(thisSelect);
						$(thisSelect).after(selectString);
					}else{
						fenleiEffection.removeSelect(thisSelect);
						$('#flid').val(pid);
					}
			});	
		}else{
			$('#flloading').remove();
			fenleiEffection.removeSelect(thisSelect);
		}
	},
	removeSelect:function(thisSelect){
		if($(thisSelect).next().is('select')){
			var nextSelect=$(thisSelect).next();
			var removeSelect=new Array();
			removeSelect.push(nextSelect);
			while(nextSelect.next().is('select')){
				nextSelect=nextSelect.next();
				removeSelect.push(nextSelect);
			}
			for(var i in removeSelect){
				removeSelect[i].remove();
			}
		}			
	}
}


var upload={
	type:'',
	formid:'',
	imgurl:'',
	setAppend:function(imgurl){
		return '<div style="margin:10px"><a href="'+imgurl+'" target="_blank"><img style="height:100px;width:100px;vertical-align:middle;border:1px #cccccc dashed" src="'+imgurl+'" /></a> 图片说明:<input type="text" name="imgIntro[]" value="" /><input type="hidden" name="imgUrl[]" value="'+imgurl+'" />　<input type="radio" value="'+imgurl+'" name="cut_url" /> 设为封面　<input type="button" value="删除" onclick="upload.delimg(this);" /></div>';
	},
	uploadimg:function(thisbutton){
		if(!this.checkUploadType($(thisbutton).prev().children().val().split('.').pop())){
			parent.showMsg('只允许上传如下图片类型: "pjpeg","gif","png","bmp","jpg","jpeg"');
			return false;
		}
		parent.showMsg('<img src="html/images/loading.gif" />');
		var oldAction=$('#'+upload.formid).attr('action');
		var oldTarget=$('#'+upload.formid).attr('target');
		$('#'+upload.formid).attr('action','adminb'+CHOSING_DE+this.type+CHOSING_DE+'uploadimg');
		$('#'+upload.formid).attr('target','productIframe');
		$('#'+upload.formid).submit();
		$('#'+upload.formid).attr('action',oldAction);
		$('#'+upload.formid).attr('target',oldTarget);
		$(thisbutton).parent().find('span').html('<input name="productpic" type="file" id="productpic" />');
	},
	setImg:function(imgurl){
		this.imgurl=imgurl;
		$('#productImages').append(this.setAppend(imgurl));
		parent.shutMsg();
		commonEffection.resetFrame('admins_frame',true);
	},
	checkUploadType:function(type){
		var allowArray=["pjpeg","gif","png","bmp","jpg","jpeg"];
		var isAllow=false;
		for(var i in allowArray){
			if(type==allowArray[i]){
				isAllow=true;
				break;
			}
		}
		return isAllow;
	},
	delimgbyid:function(thisbutton){
		if(confirm("确定删除该产品图片吗?")){
			parent.showMsg('<img src="html/images/loading.gif" />');
			var attachmentid=$(thisbutton).parent().find('input[name="attachmentid[]"]').val();
			var is_cut_url=$(thisbutton).parent().find('input[type="radio"]').attr('checked')=='true' ? 1 : 0;
			$.post(ROOT_PATH+"/adminb"+CHOSING_DE+upload.type+CHOSING_DE+"delimgbyid",{attachmentid:attachmentid,is_cut_url:is_cut_url},function(data) {
				if(trim(data)=='success'){
					parent.shutMsg('删除成功!');	
					$(thisbutton).parent().remove();
					commonEffection.resetFrame('admins_frame',true);
				}else{
					parent.alterMsg('删除失败!');	
				}
			});	
		}			
	},
	delimg:function(thisbutton){
		if(confirm("确定删除该产品图片吗?")){
			parent.showMsg('<img src="html/images/loading.gif" />');
			var imgurl=$(thisbutton).parent().find('input[name="imgUrl[]"]').val();
			$.post(ROOT_PATH+"/adminb"+CHOSING_DE+upload.type+CHOSING_DE+"delimg",{imgurl:imgurl},function(data) {
				if(data=='success'){
					parent.shutMsg('删除成功!');	
					$(thisbutton).parent().remove();
					commonEffection.resetFrame('admins_frame',true);
				}else{
					parent.alterMsg('删除失败!');	
				}
			});	
		}
	}
}

var demandUpload={
	uploadimg:function(thisbutton){
		if(!this.checkUploadType($(thisbutton).prev().children().val().split('.').pop().toLowerCase())){
			parent.showMsg('只允许上传如下图片类型: "pjpeg","gif","png","bmp","jpg","jpeg"');
			return false;
		}
		parent.showMsg('<img src="html/images/loading.gif" />');
		var oldAction=$('#demandForm').attr('action');
		var oldTarget=$('#demandForm').attr('target');
		$('#demandForm').attr('action','adminb'+CHOSING_DE+'demand'+CHOSING_DE+'uploadimg');
		$('#demandForm').attr('target','productIframe');
		$('#demandForm').submit();
		$('#demandForm').attr('action',oldAction);
		$('#demandForm').attr('target',oldTarget);
		$(thisbutton).parent().find('span').html('<input name="productpic" type="file" id="productpic" />');
	},
	setImg:function(imgurl){
		$('#productImages').append('<div style="margin:5px"><a href="'+imgurl+'" target="_blank"><img style="width:100px;height:100px;vertical-align:middle;border:1px #cccccc dashed" src="'+imgurl+'" /></a>　Intro.: <input type="text" name="imgIntro[]" value="" /><input type="hidden" name="imgUrl[]" value="'+imgurl+'" />　<input type="button" value="delete" onclick="demandUpload.delimg(this);" /></div>');
		parent.shutMsg();
		commonEffection.resetFrame('admins_frame',true);
	},
	checkUploadType:function(type){
		var allowArray=["pjpeg","gif","png","bmp","jpg","jpeg"];
		var isAllow=false;
		for(var i in allowArray){
			if(type==allowArray[i]){
				isAllow=true;
				break;
			}
		}
		return isAllow;
	},
	delimg:function(thisbutton){
		if(confirm("Sure to delete this image?")){
			parent.showMsg('<img src="html/images/loading.gif" />');
			var imgurl=$(thisbutton).prev().val();
			$.post(ROOT_PATH+"/adminb"+CHOSING_DE+"demand"+CHOSING_DE+"delimg",{imgurl:imgurl},function(data) {
				if(data=='success'){
					parent.shutMsg('Delete succeed!');	
					$(thisbutton).parent().remove();
					commonEffection.resetFrame('admins_frame',true);
				}else{
					parent.alterMsg('Delete fail!');	
				}
			});	
		}
	},
	delimgbyid:function(thisbutton){
		if(confirm("确定删除该产品图片吗?")){
			parent.showMsg('<img src="html/images/loading.gif" />');
			var attachmentid=$(thisbutton).parent().find('input[name="attachmentid[]"]').val();
			var is_cut_url=$(thisbutton).parent().find('input[type="radio"]').attr('checked')=='true' ? 1 : 0;
			$.post(ROOT_PATH+"/adminb"+CHOSING_DE+"demand"+CHOSING_DE+"delimgbyid",{attachmentid:attachmentid},function(data) {
				if(trim(data)=='success'){
					parent.shutMsg('删除成功!');	
					$(thisbutton).parent().remove();
					commonEffection.resetFrame('admins_frame',true);
				}else{
					parent.alterMsg('删除失败!');	
				}
			});	
		}			
	}
}
/*
var productEffection={
	is_showed:'',
	showAll:function(page,flid){
		$('#productRight').html('<img src="html/images/loading.gif" />');
		$.post(ROOT_PATH+"/adminb"+CHOSING_DE+"product"+CHOSING_DE+"getproductajax",{flid:flid,page:page,isshow:this.is_showed},function(data) {
			$('#productRight').html(data);
		});				
	}	
}
*/
var productEffection={
	is_showed:'',
	searchQuery:'',
	showAll:function(page,flid){
		$('#productRight').html('<img src="html/images/loading.gif" />');
		$.post(ROOT_PATH+"/adminb"+CHOSING_DE+fenleiEffection.type+CHOSING_DE+"getitemajax",{flid:flid,page:page,isshow:this.is_showed,searchQuery:this.searchQuery},function(data) {
			$('#productRight').html(data);
			commonEffection.resetFrame('admins_frame',true);
		});				
	},
	order:{
			setParam:function(){
				var orderid=trim($('input[name="orderid"]').val());
				var time_from=trim($('input[name="time_from"]').val());
				var time_to=trim($('input[name="time_to"]').val());
				var seller=trim($('input[name="seller"]').val());
				var money_from=trim($('input[name="money_from"]').val());
				var money_to=trim($('input[name="money_to"]').val());
				var flid2=trim($('select[name="flid"]').val());
				productEffection.searchQuery='{"orderid":"'+orderid+'","time_from":"'+time_from+'","time_to":"'+time_to+'","seller":"'+seller+'","money_from":"'+money_from+'","money_to":"'+money_to+'","flid2":"'+flid2+'"}';
			}
	},
	court:{
			setParam:function(){
				var who=trim($('input[name="who"]').val());
				productEffection.searchQuery='{"who":"'+who+'"}';
			}
	}
}

var headerMenu={
	flString:'',
	left:'0px',
	top:'30px',
	bgColor:'#6495AB',
	preBgColor:'',
	showCategories:function(thisA){
		$('#cateMenuDiv').css('display','block');
		if($('#floatCategoryDiv').css('display')=='block'){
			//this.removeCategories();
		}else{
		//$('#floatCategoryDiv').css('position','absolute');
			$('#floatCategoryDiv').css('margin-bottom','0px');
			//$('#floatCategoryDiv').css('left',this.left);
			//$('#floatCategoryDiv').css('top',this.top);
			$('#floatCategoryDiv').slideDown('fast');
			$('.topics,#centerBody').mouseover(
				function(){
					$('#floatCategoryDiv').slideUp('fast',function(){headerMenu.removeCategories(thisA);});
				}							
			);
		}
	},
	removeCategories:function(thisA){
		$('#floatCategoryDiv').slideUp('fast');
		$('#cateMenuDiv').css('display','none');
	}
}

var orderExpress={
	kuang:new Object(),
	innerHTML:"",
	mode:"express",
	orderid:'',
	itemid:'',
	shipping_cost:'',
	show:function(){
		this.createKuang();
		this.setKuangContent();
	},
	createKuang:function(){
		var thisOrderKuang=getCenterDiv(700,480,350,240,'thisOrderKuang');
		showBack('orderBackDiv');
		document.getElementsByTagName("body")[0].appendChild(thisOrderKuang);
		$('#thisOrderKuang').fadeIn('slow');
		$('#thisOrderKuang').css('paddingTop','0px');
		this.kuang=thisOrderKuang;
	},
	shutKuang:function(){
		shutBack('orderBackDiv');
		$(this.kuang).remove();
	},
	setKuangContent:function(){
		this.innerHTML='<div class="floatPmTop"><a style="float:right" onclick="orderExpress.shutKuang();">Close</a></div>';
		this.innerHTML+='<div class="floatPmCenter"><iframe width="700px" height="450px" src="adminb-order-'+this.mode+'?orderid='+this.orderid+'&itemid='+this.itemid+'&shipping_cost='+this.shipping_cost+'"></iframe></div>';
		this.innerHTML+='<div class="floatPmBottom"></div>';
		$(this.kuang).html(this.innerHTML);
	}
}

var buyerAddress={
	isEdit:false,
	add:function(){
		$('#addressDiv').append('<div class="addressItem"><input class="addressInput" type="text" name="address[]" value="" id="address">&nbsp;&nbsp;<img  onclick="buyerAddress.del(this)" src="html/images/adminb/shut.gif" /></div>');	
		commonEffection.resetFrame('admins_frame',true);
	},
	del:function(thisImg){
		$(thisImg).parent().remove();
		commonEffection.resetFrame('admins_frame',true);
	},
	dodel:function(thisimg,addressid){
		if(confirm('Sure to delete this address?')){
			$.post(ROOT_PATH+"/adminb-address-dodel",{addressid:addressid},function(data) {
				if(trim(data)=='success'){
					$(thisimg).parent().parent().remove();
				}
			});
		}
	},
	edit:function(thisimg,addressid){
		if(!this.isEdit){
			var value=$(thisimg).parent().prev().html();
			$(thisimg).parent().prev().html('<input class="addressInput" type="text" value="'+value+'" />&nbsp;<input type="button" value="EDIT" onclick="buyerAddress.doedit(this,\''+addressid+'\')" />');	
			this.isEdit=true;
		}
	},
	doedit:function(thisinput,addressid){
		var content=$(thisinput).prev().val();
		$.post(ROOT_PATH+"/adminb-address-doedit",{addressid:addressid,content:html2text(content)},function(data) {
			if(trim(data)=='success'){
				$(thisinput).parent().html(html2text(content));
				buyerAddress.isEdit=false;
			}
		});
	}
}

var memberBank={
	isEdit:false,
	add:function(){
		$('#bankDiv').append('<div class="bankItem"><input type="text" class="bankNameInput" name="bankName[]" value="Bank name" />&nbsp;<input class="bankAccountInput" type="text" name="bankAccount[]" value="Bank account">&nbsp;&nbsp;<img  onclick="memberBank.del(this)" src="html/images/adminb/shut.gif" /></div>');	
		$('.bankItem:last input').css('color','#999999');
		$('.bankItem:last input').click(
			function(){
				$(this).val('');
				$(this).unbind();
				$(this).css('color','#000000');
			}						   
		);
		commonEffection.resetFrame('admins_frame',true);
	},
	del:function(thisImg){
		$(thisImg).parent().remove();
		commonEffection.resetFrame('admins_frame',true);
	},
	dodel:function(thisimg,bankid){
		if(confirm('Sure to delete this bank?')){
			$.post(ROOT_PATH+"/adminb-bank-dodel",{bankid:bankid},function(data) {
				if(trim(data)=='success'){
					$(thisimg).parent().parent().remove();
				}
			});
		}
	},
	edit:function(thisimg,bankid){
		if(!this.isEdit){
			var bankName=$(thisimg).parent().prev().prev().html();
			var bankAccount=$(thisimg).parent().prev().html();
			$(thisimg).parent().prev().prev().html('<input type="text" class="bankNameInput" name="bankName[]" value="'+bankName+'" />');
			$(thisimg).parent().prev().html('<input class="bankAccountInput" type="text" name="bankAccount[]" value="'+bankAccount+'">&nbsp;<input type="button" value="EDIT" onclick="memberBank.doedit(this,\''+bankid+'\')" />');	
			this.isEdit=true;
		}
	},
	doedit:function(thisinput,bankid){
		var bank_name=$(thisinput).parent().prev().find('input').val();
		var bank_account=$(thisinput).prev().val();
		$.post(ROOT_PATH+"/adminb-bank-doedit",{bankid:bankid,bank_name:html2text(bank_name),bank_account:html2text(bank_account)},function(data) {
			if(trim(data)=='success'){
				$(thisinput).parent().prev().html(html2text(bank_name));
				$(thisinput).prev().parent().html(html2text(bank_account));
				memberBank.isEdit=false;
			}
		});
	}		
}
var subjection={
	preKeyWords:'',
	left:'233px',
	top:'71px',
	width:'345px',
	bgColor:'#ffffff',
	padding:'5px',
	data:'',
	oEvent:new Object(),
	setSubjection:function(e){
		var oEvent = e||window.event; 
		keyCode=oEvent.keyCode;
		var thisInput=$('.inputSearch');
		if(keyCode!=40&&keyCode!=38){
			this.removeSubjectionDiv();
		}
		var keyWords=trim(thisInput.val());
		if(keyWords!=''&&this.preKeyWords!=keyWords){
			this.preKeyWords=keyWords;
			this.getSubjection(keyWords,oEvent);
		}else if(keyWords==''){
			this.preKeyWords=keyWords;
		}
	},
	setDiv:function(){
		$('#subjectionRelative').append('<div id="subjectionDiv"></div>');
		$('#subjectionDiv').css('display','none');
		$('#subjectionDiv').css('position','absolute');
		$('#subjectionDiv').css('left',this.left);
		$('#subjectionDiv').css('top',this.top);
		$('#subjectionDiv').css('backgroundColor',this.bgColor);
		$('#subjectionDiv').css('width',this.width);
		//$('#subjectionDiv').css('padding',this.padding);
		$('#subjectionDiv').css('border','1px #cccccc solid');
		$('#subjectionDiv').html(this.data);
		$('#subjectionDiv').slideDown('fast');
		$('#centerBody,.popSearch').mouseover(
			function(){
				$('#subjectionDiv').slideUp('fast',function(){subjection.removeSubjectionDiv();});
			}							
		);
	},
	getSubjection:function(keyWords,e){
		var e = e||window.event;
		var mycallback="subjection.getSubjectionData";
		$.getJSON(JAVA_URL+"/search_keywords.jsp?callback=?",{keyword:keyWords,mycallback:mycallback});
	},
	getSubjectionData:function(json,e){
		var e = e||window.event;
		if(json.hasresult==1){
			var data="";
			for(var i=0;i<json.result.length;i++){
				data+='<div class="subjectionItem"><span class="floatRight">'+json.result[i].number+' items</span><span class="subjectionSpan">'+json.result[i].keyword+'</span></div>';	
			}
			subjection.data=data;
			subjection.setDiv();
			$('.inputSearch').unbind("keydown");
			$('.inputSearch').keydown(
				function(e){
					var e = e||window.event;
					subjection.upDownSelect(e);
				}						  
			);
			$('.subjectionItem').mouseover(
				function(){
					subjection.setSelected(this);
				}				  
			);
			$('.subjectionItem').click(
				function(){
					subjection.addKeywordToInput();	
				}					   
			);
		}
	},
	removeSubjectionDiv:function(){
		$('#subjectionDiv').remove();
		//$('#subjectionDiv').slideUp('fast',function(){$('#subjectionDiv').remove();});
	},
	upDownSelect:function(e){
		var oEvent = e||window.event;
		//alert(oEvent.keyCode);
		if(oEvent.keyCode==40){
			if($('.subjectionItem_change').size()==0){
				subjection.setSelected($('.subjectionItem').get(0));
			}else if($('.subjectionItem_change').next().size()==0){
				subjection.setSelected($('#subjectionDiv div:first'));
			}else{
				subjection.setSelected($('.subjectionItem_change').next().get(0));
			}
		}else if(oEvent.keyCode==38){
			if($('.subjectionItem_change').size()==0){
				subjection.setSelected($('.subjectionItem').get($('.subjectionItem').size()-1));
			}else if($('.subjectionItem_change').prev().size()==0){
				subjection.setSelected($('#subjectionDiv div:last'));
			}else{
				subjection.setSelected($('.subjectionItem_change').prev().get(0));
			}
		}else if(oEvent.keyCode==13){
			subjection.addKeywordToInput();
		}else{
			//alert(oEvent.keyCode);	
		}
	},
	setSelected:function(thisDiv){
		subjection.noSelected();
		$(thisDiv).attr('className','subjectionItem_change');
		$(thisDiv).find('span').css('color','#ffffff');
	},
	noSelected:function(){
		$('#subjectionDiv div').attr('className','subjectionItem');
		$('#subjectionDiv div').find('span').css('color','#000000');
	},
	addKeywordToInput:function(){
		this.preKeyWords=$('.subjectionItem_change').find('.subjectionSpan').html();
		if(this.preKeyWords){
			$('.inputSearch').val(this.preKeyWords);
			this.removeSubjectionDiv();
		}
	}
}

var popSearch={
	keyword:'',
	getPopSearch:function(){
		var mycallback="popSearch.getData";
		$.getJSON(JAVA_URL+"/search_keywords.jsp?callback=?",{keyword:this.keyword,mycallback:mycallback});
	},
	getData:function(json){
		if(json.hasresult==1){
			var data="";
			for(var i=0;i<json.result.length;i++){
				data+='<li class="MPLI"><a href="buyer-search-searchitem?keyword='+encodeURI(json.result[i].keyword)+'&flid=0&withWords=1">'+json.result[i].keyword+'</a></li>';	
			}
			$('#popSearchUl').html(data);
		}
	}
};

function checkinput(value)
{
	var  re = /^[A-Za-z0-9\-_@\.]*$/;
	rp = value.search(re);
	if (rp == -1) {
		GGSHOP.msgBox.show('Your input has error', 'Your input has error');		
		return false;
	}
}
